﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.DB;
using YT.Commons;
using YT.Modules;


namespace YT.XH.IgE
{
    public partial class frmPrintDgnosisRpt : Form
    {
        public frmPrintDgnosisRpt()
        {
            InitializeComponent();
        }

        private void btnLookup_Click(object sender, EventArgs e)
        {
            using (DBAccess dbcon = new DBAccess(false, false))
            {
                string strSQL = "select RecID,DetectID as 检测编号,CheckName as 检测人, CheckDate as 日期, StandardFormula as 曲线方程, ParaR as 方差 from dbo.IgEDetectHdr Order By CheckDate desc";
                frmLookUp_Cust frmlookup = new frmLookUp_Cust(dbcon, strSQL, "检测选择", "", "DetectID");
                frmlookup.ShowDialog();
                if (frmlookup.HaveReturnValue)
                {
                    txtDetectID.Text = frmlookup.GetReturnValue("DetectID");                    
                }
            }
        }

        private bool VaildCheck()
        {
            if (txtDetectID.Text == "") return false;
            return true;
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (VaildCheck())
            {
                using (DBAccess dbcon = new DBAccess(false, false))
                {
                    PrintRpt(dbcon, txtDetectID.Text);
                }
            }

        }


        private void PrintRpt(DBAccess dbcon, string strDetectID)
        {
            DataFunctions.DelTemporyTable(dbcon, "#tmpDetectInfo");
            string strSQL = @"Create Table #tmpDetectInfo ( 
                             RecID int identity(1,1), 
                             SickRecID char(16) not null default 0,                             
                             SickName char(20) not null default '',
                             CheckDate1 DateTime NULL,                             
                             CalResult1 decimal(18,8) NULL,
                             Sex char(5) not null default '',
                             Age int not null default '',
                             CheckName varchar(10) not null default '',   
                             CheckDate2 DateTime NULL,                             
                             CalResult2 decimal(18,8) NULL,
                             CheckType char(20) not null default '',
                             Remark varchar(500) not null default '')";
            dbcon.ExecuteSql(strSQL);

            string strSickName = "";
            strSQL = "Select Distinct SickPersonID From IgEDetectHdr A INNER JOIN IgEDetectDetail B on A.RecID=B.HdrRecID where A.DetectID='" + strDetectID + "' and SickPersonID!='' Order By B.SickPersonID ";
            DataTable dtSick = dbcon.GetDataTable(strSQL);
            foreach (DataRow drSick in dtSick.Rows)
            {
                strSQL = "SELECT A.CheckDate,A.CheckName,B.SickPersonID,B.CALValue,A.FormulaType From IgEDetectHdr A INNER JOIN IgEDetectDetail B on A.RecID=B.HdrRecID where B.SickPersonID='" + drSick["SickPersonID"] + "' Order By A.CheckDate";
                DataTable dtHist = dbcon.GetDataTable(strSQL);
                foreach (DataRow drRow in dtHist.Rows)
                {
                    if (CvtFunc.ToString(drRow["FormulaType"]) == FormulaType.IgE.ToString())
                    {
                        strSQL = @"Insert Into #tmpDetectInfo (CheckName,SickRecID,SickName,CheckDate1,CalResult1,CheckType)
                                   Values ('{0}','{1}','{2}','{3}','{4}','{5}')";
                    }
                    else if (CvtFunc.ToString(drRow["FormulaType"]) == FormulaType.IgG4.ToString())
                    {
                        strSQL = @"Insert Into #tmpDetectInfo (CheckName,SickRecID,SickName,CheckDate2,CalResult2,CheckType)
                                   Values ('{0}','{1}','{2}','{3}','{4}','{5}')";
                    }
                    else
                    {
                        break;
                    }

                    strSickName = CvtFunc.ToString(dbcon.GetValue("select sickname from mSickPerson where SickRecID='" + drRow["SickPersonID"] + "'"));
                    strSQL = string.Format(strSQL, CvtFunc.ToString(drRow["CheckName"])
                                                    , drRow["SickPersonID"]
                                                    , strSickName
                                                    , BaseFunctions.FormatDate(drRow["CheckDate"].ToString(), DateFormatOption.DateFmtMDY)
                                                    , CvtFunc.ToDouble(drRow["CALValue"])
                                                    , CvtFunc.ToString(drRow["FormulaType"])
                                           );
                    dbcon.ExecuteSql(strSQL);

                    strSQL = "Update #tmpDetectInfo Set Sex=B.Sex, Age=B.Age from mSickPerson B where #tmpDetectInfo.SickRecID=B.SickRecID";
                    dbcon.ExecuteSql(strSQL);
                }
            }
            PrintInfo printInfo = new PrintInfo();
            Reports.Report rpt = new Reports.Report();
            printInfo.ReportPageTitle = SysVar.HospitalName+ "\r\n"+ "  屋尘螨IgG4检测报告  ";
            rpt.OeDailyCashSalesPaymentReport(dbcon, printInfo);

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }
    }
}
